import {
  createRouter,
  createWebHashHistory,
  createWebHistory,
  type RouteRecordRaw,
} from "vue-router";
import NProgress from "nprogress";
import { Message } from "@arco-design/web-vue";
import { userStoreModel } from "@/stores/user/index.ts";
import { storeToRefs } from "pinia";

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    redirect: "/web",
  },
  {
    path: "/admin",
    name: "Admin",
    component: () => import("@/views/admin/index.vue"),
    children: [
      {
        path: "",
        name: "Home",
        component: () => import("@/views/admin/home/index.vue"),
        meta: {
          title: "首页",
          role: [1, 2, 3],
        },
      },
      {
        path: "userCenter",
        name: "UserCenter",
        meta: {
          title: "个人中心",
          role: [1, 2, 3],
        },
        component: () => import("@/views/admin/user_center/index.vue"),
        children: [
          {
            path: "info",
            name: "UserInfo",
            meta: {
              title: "用户信息",
            },
            component: () =>
              import("@/views/admin/user_center/user_info/index.vue"),
          },
        ],
      },
      {
        path: "userManage",
        name: "UserManage",
        meta: {
          title: "用户管理",
          role: [1],
        },
        component: () => import("@/views/admin/user_manage/index.vue"),
        children: [
          {
            path: "list",
            name: "UserList",
            meta: {
              title: "用户列表",
            },
            component: () =>
              import("@/views/admin/user_manage/user_list/index.vue"),
          },
        ],
      },
      {
        path: "componentManage",
        name: "ComponentManage",
        meta: {
          title: "组件管理",
          role: [1],
        },
        component: () => import("@/views/admin/component_manage/index.vue"),
        children: [
          {
            path: "list",
            name: "ComponentList",
            meta: {
              title: "组件列表",
              role: [1],
            },
            component: () =>
              import("@/views/admin/component_manage/component_list/index.vue"),
          },
        ],
      },
      {
        path: "settingsManage",
        name: "SettingsManage",
        meta: {
          title: "系统管理",
          role: [1],
        },
        component: () => import("@/views/admin/settings_manage/index.vue"),
        children: [
          {
            path: "info",
            name: "SettingsInfo",
            meta: {
              title: "系统信息",
              role: [1],
            },
            component: () =>
              import("@/views/admin/settings_manage/settings_info/index.vue"),
          },
        ],
      },
    ],
  },
  {
    path: "/web",
    name: "Web",
    component: () => import("@/views/web/index.vue"),
    children: [
      {
        path: "",
        name: "WebHome",
        component: () => import("@/views/web/home/index.vue"),
        meta: {
          title: "首页",
        },
      },
      {
        path: "news",
        name: "WebNews",
        component: () => import("@/views/web/news/index.vue"),
        meta: {
          title: "新闻",
        },
      },
      {
        path: "jsbase",
        name: "WebJsbase",
        component: () => import("@/views/web/jsbase/index.vue"),
        meta: {
          title: "JS基础",
        },
      },
    ],
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("@/views/login/index.vue"),
  },
  {
    path: "/:match(.*)*",
    name: "notfound",
    component: () => import("@/views/web/404.vue"),
  },
];

const router = createRouter({
  // history: createWebHistory(import.meta.env.BASE_URL),
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  const userStore = userStoreModel();
  const { userInfo } = storeToRefs(userStoreModel());
  console.log("用户信息：", userInfo.value);

  if (to.meta.role) {
    if (!userStore.isLogin) {
      Message.error("请先登录");
      router.push({ name: "Login", query: { redirect: to.path } });
      return;
    }

    // 路由权限管理
    if (!to.meta?.role.includes(userStore.userInfo.role)) {
      Message.error("鉴权失败");
      router.push(from.path);
      return;
    }
  }

  NProgress.configure({ showSpinner: false });
  NProgress.start();
  next();
});
router.afterEach(() => {
  NProgress.done();
});

export default router;
